/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Presentation;

import BussinesLogic.BL_Colaborador;
import BussinesLogic.BL_Arbitro;
import BussinesLogic.Request;
import javax.swing.JOptionPane;

public class Login extends javax.swing.JFrame {

    public Login() {
        initComponents();
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelUsername = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        labelRol = new javax.swing.JLabel();
        comboRol = new javax.swing.JComboBox();
        campoUsername = new javax.swing.JTextField();
        bOK = new javax.swing.JButton();
        campoPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("INICIO DE SESION");
        setResizable(false);

        labelUsername.setText("NOMBRE DE USUARIO");

        labelPassword.setText("CONTRASEÑA");

        labelRol.setText("ROL");

        comboRol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Arbitro", "Colaborador" }));

        bOK.setText("Iniciar Sesion");
        bOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUsername)
                            .addComponent(labelPassword)
                            .addComponent(labelRol))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(campoPassword, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(campoUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(bOK)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUsername)
                    .addComponent(campoUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPassword)
                    .addComponent(campoPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelRol)
                    .addComponent(comboRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(bOK)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOKActionPerformed
        // TODO add your handling code here:
        if (campoUsername.getText().isEmpty() || campoPassword.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Por favor diligenciar todos los campos",
                    "JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else {
            int tipoRol = comboRol.getSelectedIndex();
            Request rqs = new Request();
            if (tipoRol == 0) {
                BL_Arbitro bl_Arbitro = new BL_Arbitro();
                rqs = bl_Arbitro.autenticarArbitro(campoUsername.getText(), campoPassword.getText());
                if (!rqs.getTransac()) {
                    JOptionPane.showMessageDialog(this, rqs.getMessage(),
                            "JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
                }
            } else if (tipoRol == 1) {
                
                BL_Colaborador bl_colaborador = new BL_Colaborador();
                rqs = bl_colaborador.autenticarColaborador(campoUsername.getText(), campoPassword.getText());
                
                if (!rqs.getTransac()) JOptionPane.showMessageDialog(this, rqs.getMessage(),
                            "JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
                else {
                    DataModel.Colaborador obj = (DataModel.Colaborador) rqs.getObject();
                    
                    if (obj.getCargo().equals("Director Deportivo")){
                        this.dispose();
                        new GUIdirDeportivo(obj.getEvento().getId());
                    }
                    else if (obj.getCargo().equals("Director de Logistica")){
                        this.dispose();
                        new GUILogistica(obj.getEvento().getId());
                    }
                }
            }
        }
    }//GEN-LAST:event_bOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bOK;
    private javax.swing.JPasswordField campoPassword;
    private javax.swing.JTextField campoUsername;
    private javax.swing.JComboBox comboRol;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelRol;
    private javax.swing.JLabel labelUsername;
    // End of variables declaration//GEN-END:variables
}
